(function(){

	var db = require("../db/index");
	var Goods = require("../goods/_con");
	var config = require("../_include/config");
	var skill = {
		addAch:function(ach_id){
			var self = this;
			self.ach.push(ach_id);
			db.update(config.dbname.bus,self._id,self);
		},
		findOneGoodsById:function(goods_id){
			var self = this;
			var store = self.store;
			var result;
			store.every(function(goods){
				if (goods._id===goods_id) {
					result = goods
					return false;
				}else{
					return true;
				}
			});
			if (result) {
				console.log(self.info.name,"从仓库中找出编号为：“",goods_name,"””的商品");
			}else{
				console.log(self.info.name,"在仓库中找不到编号为：“",goods_name,"””的商品");
			}
			return result;
		},
		findOneGoodsByName:function(goods_name){
			var self = this;
			var store = self.store;
			var result;
			store.every(function(goods){
				if (goods.info.name === goods_name) {
					result = goods;
					return false;
				}else{
					return true;
				}
			});
			if (result) {
				console.log(self.info.name,"从仓库中找出商品名字为：“",goods_name,"”的商品");
			}else{
				console.log(self.info.name,"在仓库中找不到商品名字为：“",goods_name,"”的商品");
			}
			return result;
		},
		//通过商品编号生成订单
		//return ach
		makeAchByGoodsId:function(user,goods_id){
			var self = this;
			var store = self.store;
			var result;
			store.every(function(goods,index,store){
				if (goods._id === goods_id) {
					store.splice(index,1);
					var Ach = require("../ach/_con");
					result = new Ach({
						user_id:user._id,
						goods_id:goods._id,
						bus_id:self._id,
						invoice:{
							price:goods.info.price,
							user_name:user.info.name,
							bus_name:self.info.name
						},
						address:{
							//省、市、县、镇、街道、号数
						},
						state:"未付款",
						payer_id:undefined,
						pay_time:undefined,
						time:+new Date
					});
					self.ach.push(result._id);
					return false;
				}else{
					return true;
				}
			});
			if (result) {
				console.log("生成订单号为：“",result._id,"”");
			}else{
				console.log("订单生产失败，找不到相关商品号为“",goods_id,"”的商品");
				throw "找不到指定商品";
			}
			return result;
		},
		//收到用户确认的订单
		//推送消息给商家
		receiveConfirmAch:function(ach_id){
			var self = this;
			self.addAch(ach_id);
			console.log("商家收到订单“",ach_id,"”！");
		},
		//解锁（退回）订单，使得用户有权限修改订单
		unlockAch:function(ach_id){
			var ach = AchCon.get(ach_id);
			ach.unlock();
			console.log("商家解锁订单“",ach_id,"”！");
		},
		//进货
		//需要拓展的细节包括：自动归类到各种分支产品中
		importGoods:function (goods_info,num) {
			var self = this;
			num || (num = 1);
			for(var i = 0; i<num ; i+=1){
				self.store.push(new Goods(goods_info,self._id));
			}
			console.log("添加货物库存：", goods_info.name, num, "件");
		},
		//设置商品的推荐积分
		setRecommendGoodsInt:function(goods_name,intValue){
			var rg_info = db.find_one({
				goods_name:goods_name
			});
			if (!rg_info) {
				rg_info = {
					_id:"RGI_"+(+new Date).toString(16),
					goods_name:goods_name,
					intValue:intValue
				}
			}
			console.log("设置商品“",goods_name,"”的推荐积分为：",intValue);
		},
		//获取商品的推荐积分
		getRecommendGoodsInt:function(goods_name){
			var rg_info = db.find_one(config.dbname.RecommendGoodsInt,{
				goods_name:goods_name
			});
			return rg_info?rg_info.intValue:0;
		},
		/*
		//为购买推荐商品的推荐用户 返利 推荐积分
		_returnRecommendGoodsInt:function(viewer_id,url_hash,rgu_info){
			var self = this;
			if (!rgu_info) {
				rgu_info = db.find_one(config.dbname.RecommendGoodsURL,{
					url_hash:url_hash
				});
			}
			if (!rgu_info) {
				return;
			}
			recommender = UserCon.get(rgu_info.user_id);
			recommender._rec_int(self.getRecommendGoodsInt(rgu_info.goods_name))
		}*/
		//为用户添加“推荐积分”
		//这里另外写一个方法进行封装是为了记录操作日志，确保积分的来源可寻
		_IncRecIntToUser:function(user_id,intValue){
			var user = UserCon.get(user_id);
			user._recInt(intValue);
		}
	};
	module.exports = skill;

}());
